<?php echo $header; ?>
<div id="content">
<div class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <?php echo $breadcrumb['separator']; ?>
    	<?php if(isset($breadcrumb['href']) && $breadcrumb['href'] != '' && $breadcrumb['href'] != '#') {?>
	    	<a href="<?php echo $breadcrumb['href']; ?>">
	    		<?php echo $breadcrumb['text']; ?>
	    	</a>
    	<?php }else{?>
    		<?php echo $breadcrumb['text']; ?>
    	<?php }?>
    <?php } ?>
  </div>
<?php if ($error_warning) { ?>
<div class="warning"><?php echo $error_warning; ?></div>
<?php } ?>
<div class="box">
  <div class="heading">
    <h1><img src="view/image/module.png" alt="" /> <?php echo $heading_title; ?></h1>
    <div class="buttons"><a onclick="$('#form').submit();" class="button"><?php echo $button_save; ?></a><a onclick="location = '<?php echo $cancel; ?>';" class="button"><?php echo $button_cancel; ?></a></div>
  </div>
  <div class="content">
    <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
      <table id="module" class="list">
        <thead>
          <tr>
            <td class="left" style="width:150px"><?php echo $entry_layout; ?></td>
            <td class="left"><?php echo $entry_staff; ?></td>
            <td class="left" style="width:120px"><?php echo $entry_position; ?></td>
            <td class="left" style="width:100px"><?php echo $entry_status; ?></td>
            <td class="center" style="width:100px"><?php echo $entry_sort_order; ?></td>
            <td></td>
          </tr>
        </thead>
        <?php $module_row = 0; ?>
        <?php $staff_row = 0; ?>
        <?php foreach ($modules as $module) { ?>
        <tbody id="module-row<?php echo $module_row; ?>">
          <tr>
            <td class="left"><select name="im_support_module[<?php echo $module_row; ?>][layout_id]">
                <?php foreach ($layouts as $layout) { ?>
                <?php if ($layout['layout_id'] == $module['layout_id']) { ?>
                <option value="<?php echo $layout['layout_id']; ?>" selected="selected"><?php echo $layout['name']; ?></option>
                <?php } else { ?>
                <option value="<?php echo $layout['layout_id']; ?>"><?php echo $layout['name']; ?></option>
                <?php } ?>
                <?php } ?>
              </select></td>
            <td class="left">
		    	<table id="staff<?php echo $module_row; ?>" style="margin-top:5px" class="list">
	    			<thead>
	    				<tr>
		    				<td class="left" style="width:150px"><?php echo $entry_staff_name; ?></td>
		    				<td class="left" style="width:150px"><?php echo $entry_nick_name; ?></td>
		 	   				<td class="left" style="width:100px"><?php echo $entry_nick_type; ?></td>
		 	   				<td class="left" style="width:100px"></td>
		 	   			</tr>
	    			</thead>
	    			<?php if(isset($module['staff'])) {?>
	    			<?php foreach ($module['staff'] as $staff){?>
			    		<tbody id="staff-row<?php echo $staff_row; ?>">
			    			<tr>
			    				<td class="left">
			    					<input type="text" name="im_support_module[<?php echo $module_row; ?>][staff][<?php echo $staff_row; ?>][staff_name]" value="<?php echo $staff['staff_name'];?>" size="15" />
			    				</td>
			    				<td class="left">
			    					<input type="text" name="im_support_module[<?php echo $module_row; ?>][staff][<?php echo $staff_row; ?>][nick_name]" value="<?php echo $staff['nick_name'];?>" size="15" />
			    				</td>
			    				<td class="left">
			    					<select name="im_support_module[<?php echo $module_row; ?>][staff][<?php echo $staff_row; ?>][nick_type]">
			    						<?php if($staff['nick_type'] == 'yahoo'){?>
			      						<option value="yahoo" selected="selected">Yahoo</option>
			      						<option value="skype">Skype</option>
			      						<?php }else{?>
			      						<option value="yahoo">Yahoo</option>
			      						<option value="skype" selected="selected">Skype</option>
			      						<?php }?>
			    					</select>
			    				</td>
			    				<td class="center" style="width:100px"><a onclick="$('#staff-row<?php echo $staff_row; ?>').remove();" class="button"><?php echo $button_remove; ?></a></td>
			    			</tr>
			    		</tbody>
			    		<?php $staff_row++;?>
	    			<?php }?>
	    			<?php }?>
		    		<tfoot id='tfoot-staff'>
		    			<tr>
		    				<td colspan="3"></td>
		    				<td class="center" style="width:120px"><a onclick="addStaff(<?php echo $module_row; ?>);" class="button"><?php echo $button_add_staff; ?></a></td>
		    			</tr>
		    		</tfoot>
		    	</table>
            </td>
            <td class="left"><select name="im_support_module[<?php echo $module_row; ?>][position]">
                <?php if ($module['position'] == 'content_top') { ?>
                <option value="content_top" selected="selected"><?php echo $text_content_top; ?></option>
                <?php } else { ?>
                <option value="content_top"><?php echo $text_content_top; ?></option>
                <?php } ?>  
                <?php if ($module['position'] == 'content') { ?>
                <option value="content" selected="selected"><?php echo $text_content; ?></option>
                <?php } else { ?>
                <option value="content"><?php echo $text_content; ?></option>
                <?php } ?>  
                <?php if ($module['position'] == 'content_bottom') { ?>
                <option value="content_bottom" selected="selected"><?php echo $text_content_bottom; ?></option>
                <?php } else { ?>
                <option value="content_bottom"><?php echo $text_content_bottom; ?></option>
                <?php } ?>     
                <?php if ($module['position'] == 'column_left') { ?>
                <option value="column_left" selected="selected"><?php echo $text_column_left; ?></option>
                <?php } else { ?>
                <option value="column_left"><?php echo $text_column_left; ?></option>
                <?php } ?>
                <?php if ($module['position'] == 'column_right') { ?>
                <option value="column_right" selected="selected"><?php echo $text_column_right; ?></option>
                <?php } else { ?>
                <option value="column_right"><?php echo $text_column_right; ?></option>
                <?php } ?>
              </select></td>
            <td class="left"><select name="im_support_module[<?php echo $module_row; ?>][status]">
                <?php if ($module['status']) { ?>
                <option value="1" selected="selected"><?php echo $text_enabled; ?></option>
                <option value="0"><?php echo $text_disabled; ?></option>
                <?php } else { ?>
                <option value="1"><?php echo $text_enabled; ?></option>
                <option value="0" selected="selected"><?php echo $text_disabled; ?></option>
                <?php } ?>
              </select></td>
            <td class="right"><input type="text" name="im_support_module[<?php echo $module_row; ?>][sort_order]" value="<?php echo isset($module['sort_order'])? $module['sort_order'] : 0; ?>" size="3" /></td>
            <td class="center"><a onclick="$('#module-row<?php echo $module_row; ?>').remove();" class="button"><?php echo $button_remove; ?></a></td>
          </tr>
        </tbody>
        <?php $module_row++; ?>
        <?php } ?>
        <tfoot id="tfoot-module">
          <tr>
            <td colspan="5"></td>
            <td class="center" style="width:120px"><a onclick="addModule();" class="button"><?php echo $button_add_module; ?></a></td>
          </tr>
        </tfoot>
      </table>
    </form>
  </div>
</div>
<script type="text/javascript"><!--
var module_row = <?php echo $module_row; ?>;
var staff_row = <?php echo $staff_row; ?>;

function addModule() {
	html  = '<tbody id="module-row' + module_row + '">';
	html += '  <tr>';
	html += '    <td class="left"><select name="im_support_module[' + module_row + '][layout_id]">';
	<?php foreach ($layouts as $layout) { ?>
	html += '      <option value="<?php echo $layout['layout_id']; ?>"><?php echo $layout['name']; ?></option>';
	<?php } ?>
	html += '    </select></td>';
	html += '    <td>';
	html += '    	<table id="staff' + module_row + '" style="margin-top:5px" class="list">';
	html += '    		<thead>';
	html += '    			<tr>';
	html += '    				<td class="left" style="width:150px"><?php echo $entry_staff_name; ?></td>';
	html += '    				<td class="left" style="width:150px"><?php echo $entry_nick_name; ?></td>';
	html += ' 	   				<td class="left" style="width:100px"><?php echo $entry_nick_type; ?></td>';
	html += ' 	   				<td class="left" style="width:100px"></td>';
	html += '    			</tr>';
	html += '    		</thead>';
	html += '    		<tfoot>';
	html += '    			<tr>';
	html += '    				<td colspan="3"></td>';
	html += '    				<td class="center" style="width:120px"><a onclick="addStaff(' + module_row + ');" class="button"><?php echo $button_add_staff; ?></a></td>';
	html += '    			</tr>';
	html += '    		</tfoot>';
	html += '    	</table>';
	html += '    </td>';
	html += '    <td class="left"><select name="im_support_module[' + module_row + '][position]">';
	html += '      <option value="content_top"><?php echo $text_content_top; ?></option>';
	html += '      <option value="content"><?php echo $text_content; ?></option>';
	html += '      <option value="content_bottom"><?php echo $text_content_bottom; ?></option>';
	html += '      <option value="column_left"><?php echo $text_column_left; ?></option>';
	html += '      <option value="column_right"><?php echo $text_column_right; ?></option>';
	html += '    </select></td>';
	html += '    <td class="left"><select name="im_support_module[' + module_row + '][status]">';
    html += '      <option value="1" selected="selected"><?php echo $text_enabled; ?></option>';
    html += '      <option value="0"><?php echo $text_disabled; ?></option>';
    html += '    </select></td>';
	html += '    <td class="center"><input type="text" name="im_support_module[' + module_row + '][sort_order]" value="" size="3" /></td>';
	html += '    <td class="center"><a onclick="$(\'#module-row' + module_row + '\').remove();" class="button"><?php echo $button_remove; ?></a></td>';
	html += '  </tr>';
	html += '</tbody>';
	
	$('#module #tfoot-module').before(html);

	addStaff(module_row);
	
	module_row++;
}

function addStaff(current_module_row){
	html = '    		<tbody id="staff-row' + staff_row + '">';
	html += '    			<tr>';
	html += '    				<td class="left">';
	html += '    					<input type="text" name="im_support_module[' + current_module_row + '][staff][' + staff_row + '][staff_name]" value="" size="15" />';
	html += '    				</td>';
	html += '    				<td class="left">';
	html += '    					<input type="text" name="im_support_module[' + current_module_row + '][staff][' + staff_row + '][nick_name]" value="" size="15" />';
	html += '    				</td>';
	html += '    				<td class="left">';
	html += '    					<select name="im_support_module[' + current_module_row + '][staff][' + staff_row + '][nick_type]">';
	html += '      						<option value="yahoo">Yahoo</option>';
	html += '      						<option value="skype">Skype</option>';
	html += '    					</select>';
	html += '    				</td>';
	html += '    				<td class="center" style="width:100px"><a onclick="$(\'#staff-row' + staff_row + '\').remove();" class="button"><?php echo $button_remove; ?></a></td>';
	html += '    			</tr>';
	html += '    		</tbody>';

	$('#staff' + current_module_row + ' #tfoot-staff').before(html);
	
	staff_row++;
}
//--></script>
<?php echo $footer; ?>